package org.swimv2.servlet;

import java.io.IOException;
import java.util.Collection;
import java.util.Properties;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.rmi.PortableRemoteObject;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.swimv2.controller.GestioneAbilita;
import org.swimv2.controller.GestioneAbilitaRemote;
import org.swimv2.controller.GestioneLavoro;
import org.swimv2.controller.GestioneLavoroRemote;
import org.swimv2.exception.LavoroNonValidoException;
import org.swimv2.model.Lavoro;
import org.swimv2.model.Persona;
import org.swimv2.util.FormNuovoLavoro;



public class CreaLavoroServlet extends HttpServlet{

	private static final long serialVersionUID = 1L;
	
	/**
     * @see HttpServlet#HttpServlet()
     */
    public CreaLavoroServlet() {
        super();
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("UTF-8");
        Persona u = (Persona) request.getSession().getAttribute("utente");

        Properties props = new Properties();
		props.put(Context.INITIAL_CONTEXT_FACTORY,"org.jnp.interfaces.NamingContextFactory");
		props.put(Context.URL_PKG_PREFIXES, "org.jboss.naming:org.jnp.interfaces");
		props.put(Context.PROVIDER_URL, "jnp://localhost:1099");
		Context ctx;
		try{
			ctx = new InitialContext(props);
			Object obj = ctx.lookup(GestioneAbilita.JNDI_NAME);
			GestioneAbilitaRemote managerAbilita = (GestioneAbilitaRemote)PortableRemoteObject.narrow(obj,GestioneAbilitaRemote.class);
			Collection<String> listAb=managerAbilita.getListaAbilita();
			request.setAttribute("listAb", listAb);
		}catch(NamingException e){
			e.printStackTrace();}	
        
		if(u != null) {
			request.getRequestDispatcher("WEB-INF/creaLavoro.jsp").forward(request, response);
		}
	}
	
	
	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("UTF-8");
        
        Properties props = new Properties();
		props.put(Context.INITIAL_CONTEXT_FACTORY,"org.jnp.interfaces.NamingContextFactory");
		props.put(Context.URL_PKG_PREFIXES, "org.jboss.naming:org.jnp.interfaces");
		props.put(Context.PROVIDER_URL, "jnp://localhost:1099");
		Context ctx;
		try{
			ctx = new InitialContext(props);
			Object obj = ctx.lookup(GestioneLavoro.JNDI_NAME);
			GestioneLavoroRemote manager = (GestioneLavoroRemote)PortableRemoteObject.narrow(obj,GestioneLavoroRemote.class);
			
			String nome = request.getParameter("nome");
			String sede = request.getParameter("sede");
			String ab[] = request.getParameterValues("ab");
			
			RequestDispatcher disp;
			Persona u = (Persona) request.getSession().getAttribute("utente");
			disp = request.getRequestDispatcher("/VisualizzaLavoro");
			Context ctx2;
			try{
				
				FormNuovoLavoro nuovoLavoro=new FormNuovoLavoro(nome,sede,u.getUserID(),ab);
				Lavoro lavoro = manager.validaLavoro(nuovoLavoro);
				request.setAttribute("lavoroTemp",lavoro);
				disp.forward(request, response);
			}
			catch(LavoroNonValidoException e){
				ctx2 = new InitialContext(props);
				Object obj2 = ctx2.lookup(GestioneAbilita.JNDI_NAME);
				GestioneAbilitaRemote managerAbilita = (GestioneAbilitaRemote)PortableRemoteObject.narrow(obj2,GestioneAbilitaRemote.class);
				Collection<String> listAb=managerAbilita.getListaAbilita();
				request.setAttribute("listAb", listAb);
				request.setAttribute("messaggio", e.getError());
				disp = request.getRequestDispatcher("WEB-INF/creaLavoro.jsp");
				disp.forward(request, response);
			}		
		} catch (NamingException e) {
			e.printStackTrace();
		}
		
	}
	

}
